<?php

/**
 * page actions.
 *
 * @package    DUYTAN
 * @subpackage page
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class informationsActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
	$this->information_form = new AdminInformationForm();
	if ($request->hasParameter('create') && $request->getParameter('create') == 'Create') {
		$res = $request->getParameter('information');
		$this->is_create_action = true;
		if ($res['name'] != '') {
			$information = new DtInformation();
			$c = Doctrine_Core::getTable('DtInformation')->findOneByName($res['name']);
			if (!$c) {
				$information->name = $res['name'];
				$information->value = $res['value'];
				$information->created_at = date(DATE_ISO8601);
				$information->updated_at = date(DATE_ISO8601);
				try {
					$information->save();
					$this->create_success = true;
				} catch (sfException $e) {
					echo $e->getMessage();
				}
			} else {
				$this->create_success = false;
			}
		}
		
	} else {
		$q = Doctrine_Query::create()
			->from('DtInformation c');

		$res = $request->getParameter('information');
		$q = $this->execFilers($q, $res);
		$this->sort_by = $request->getParameter('sort_by', 'created_at');
    	if ($request->getParameter('sort_order', 1))
        	$this->sort_order = 'desc';
    	else
        	$this->sort_order = 'asc';
    	if (Doctrine_Core::getTable('DtInformation')->hasField($this->sort_by))
        	$q->orderBy('c.'.$this->sort_by.' '.strtoupper($this->sort_order));
		$this->pager = new sfDoctrinePager('DtInformation', 20);
		$this->pager->setQuery($q);
    	$this->pager->setPage($request->getParameter('page', 1));
    	$this->pager->init();
		if ($request->isXmlHttpRequest()) {
        	return 'Ajax';
    	} else return SfView::SUCCESS;
	}
  }


	private function execFilers($q, $res)
	{
		if ($res['name'] != '')
			$q->addWhere('c.name LIKE "%'.$res['name'].'%"');
		if ($res['value'] != '')
			$q->addWhere('c.value LIKE "%'.$res['value'].'%"');
		return $q;
	}
}
